<template>
	<view class="page" :style="{minHeight:$height()}">
		<view class="searchBox centerFlex">
			<view class="icon">
				<image src="@/static/Icon/serch.png" mode="" class="icon"></image>
			</view>
			<input placeholder="请输入企业信息" v-model="keywords" class="sch" />
			<view @click="search" class="txt">
				搜索
			</view>
		</view>

		<view class="history-list" v-if="showList == false">
			<view class="title">搜索历史</view>

			<view class="items">
				<view class="item" v-for="(item,index) in historyList" :key="index">
					<text class="keywords" @click="doSearch(item.keywords)">{{item.keywords}}</text>
				</view>

			</view>
		</view>

		<view class="companys" v-if="showList == true">
			<view class="company" v-for="(item,index) in list" :key="index" @click="choose(item)">
				<view class="title">
					{{item.companyName}}
				</view>
				<view class="other">
					法人：{{item.companyPerson}} | {{item.companyAddress}}
				</view>
			</view>
			<view class="more" v-if="more == false && tmore == true">
				<text @click="lookMore">点击查看更多</text>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				keywords: '',
				page: 1,
				tpage: 1, //天眼查页码
				tlimit: 20, //天眼查每页条数
				tmore: true, //天眼查数据是否还有更多
				historyList: [], //历史记录
				list: [],
				showList: false,
				more: false,
			}
		},
		onShow() {
			this.getHistoryRes()
		},
		//下拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.search()
		},
		//上拉加载
		onReachBottom() {
			if (this.page == 1) {
				return
			}
			if (this.more) {
				this.search()
			}
		},
		methods: {
			choose(item) {
				uni.$emit('chooseCompany', item)
				this.$nextTick(() => {
					uni.navigateBack()
				})
			},
			getHistoryRes() {
				this.$axios.request(this.$api.getHistoryRes, "GET", {
					page: 1,
					limit: 20,
					orderField: 'update_date',
					order: 'desc'
				}).then((val) => {
					this.historyList = val.data.list
				})
			},
			doSearch(keywords) {
				this.keywords = keywords
				this.page = 1
				this.search()
			},
			lookMore() {
				this.$axios.request(this.$api.getTianyanchaList, "GET", {
					keywords: this.keywords,
					page: this.tpage,
					limit: this.tlimit
				}).then((val) => {
					var data = JSON.parse(val.data)
					console.log(data)
					data.result.items.forEach((item, index) => {
						this.list.push({
							companyName: item.name,
							companyPerson: item.legalPersonName,
							companyAddress: item.base,
							creditCode: item.creditCode
						})
					})

					if (Math.ceil(data.result.total / this.tlimit) > this.page && data.result.items.length == this
						.tlimit) {
						this.tmore = true
						this.tpage++
					} else {
						this.tmore = false
					}


				})
			},
			search() {
				this.showList = true
				this.$axios.request(this.$api.search, "GET", {
					page: this.page,
					limit: 10,
					keywords: this.keywords,
					orderField: 'id',
					order: 'desc'
				}).then((val) => {
					uni.stopPullDownRefresh();
					this.list = this.page == 1 ? val.data.list : this.list.concat(val.data.list)
					if (Math.ceil(val.data.total / this.limit) > this.page && val.data.list.length == 10) {
						this.more = true;
						this.page++
					} else {
						this.more = false;
					}

					this.getHistoryRes()
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "../../style/contentFlex.css";

	.page {
		width: 100%;
		/* height: 100vh; */
		background-color: #FFFFFF;
		overflow: hidden;
		padding-bottom: 16rpx;
	}

	.searchBox {
		width: 702rpx;
		height: 72rpx;
		background: rgba(0, 0, 0, 0.06);
		margin: 0 auto 0;

		.icon {
			width: 34rpx;
			height: 28rpx;
		}

		.sch {
			width: 546rpx;
			height: 40rpx;
			margin: 0 16rpx;
			border-right: 2rpx solid #A8B5C5;
		}

		.txt {
			font-size: 28rpx;
			font-family: "PingFang SC";
			font-weight: 300;
			line-height: 60rpx;
			color: #3975EA;
			opacity: 1;
		}
	}

	.history-list {
		padding: 35rpx;

		.title {
			font-size: 24rpx;
			font-weight: bold;
			line-height: 55rpx;
		}

		.keywords {
			padding: 10rpx;
			background-color: #f5f5f5;
			border-radius: 5rpx;
			font-size: 22rpx;
		}

		.leading-in {
			padding: 10rpx 15rpx;
			color: #2e6adf;
			border-radius: 5rpx;
			border: 1px solid #2e6adf;
			font-size: 22rpx;
		}

		.item:first-child {
			margin-top: 20rpx;
		}

		.item {
			margin-top: 35rpx;
		}

	}

	.companys {
		padding: 35rpx;

		.title {
			color: #333333;
			font-size: 26rpx;
			line-height: 48rpx;
		}

		.other {
			color: #999999;
			font-size: 16rpx;
		}

		.company {
			margin-bottom: 20rpx;
		}
	}

	.more {
		display: flex;
		justify-content: center;

		text {
			color: #2e6adf;
			padding: 10rpx 30rpx;
			border: 1px solid #2e6adf;
			font-size: 24rpx;
		}
	}
</style>
